package com.hnkywl.sts.service.jsgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.cggl.GlfyDao;
import com.hnkywl.sts.dao.cggl.GlfymxDao;
import com.hnkywl.sts.dao.cwgl.kmys.CyfyysDao;
import com.hnkywl.sts.dao.jsgl.JsdfymxDao;
import com.hnkywl.sts.dao.jsgl.WljsdDao;
import com.hnkywl.sts.dao.jsgl.WljsdmxDao;
import com.hnkywl.sts.dao.jsgl.ZjbcdDao;
import com.hnkywl.sts.entity.cggl.*;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-2
 * Time: 下午4:17
 * To change this template use File | Settings | File Templates.
 */
@Service
public class WljsdService {
    private WljsdDao wljsdDao;
    private GlfyDao glfyDao;
    private WljsdmxDao wljsdmxDao;
    private JsdfymxDao jsdfymxDao;
    private CyfyysDao cyfyysDao;
    private GlfymxDao glfymxDao;
    private BmService bmService;
    private ZjbcdDao zjbcdDao;

    @Transactional
    public String findAllWljsd(Integer zt, String jsdh, Date sqrqStart, Date sqrqEnd, Long glqyId, String glqyQc, Long gsId, String gsJc, Long fpzlId, int pageNo, int pageSize) {
        if ("".equals(gsJc)) {
            gsId = null;
        }
        Page<Jsd> wljsdPage = this.wljsdDao.findWljsdList(zt, jsdh, sqrqStart, sqrqEnd, glqyId, glqyQc, gsId, gsJc, fpzlId, pageNo, pageSize);
        List<Jsd> wljsdList = wljsdPage.getResult() == null ? new ArrayList<Jsd>() : wljsdPage.getResult();
        String wljsdJson = BaseUtils.fetchCollectionToJson(wljsdList, "id", "jsdh", "sqrq", "glqy.qyfr.nsh", "type", "glqy.id", "glqy.qc", "fpze", "ddfy", "fpzl", "hm", "sqr.id", "sqr.xm", "gs.id", "gs.jc", "zt", "fpthyy", "sftzd");
        return "{\"total\":" + wljsdPage.getTotalCount() + ",\"rows\":[" + wljsdJson + "]}";
    }

    /**
     * 保存物流单结算
     *
     * @param wljsd
     */
    @Transactional
    public void saveWljsd(Jsd wljsd) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0000");
        java.text.DecimalFormat df2 = new java.text.DecimalFormat("#0.00");
        initWljsd(wljsd);
        wljsd.setJsdh(this.bmService.getJsdbm("wljs"));
        wljsd.setZt(0); //0-未提交 1-已退回 2-已提交 3-已审核
        wljsd.setShzt(0); //0-未审核 1-已审核
        wljsd.setSqrq(new Date());
        wljsd.setType(2); //type=0 采购 ; 1 销售，2物流
        wljsd.setDateCreated(new Date());
        wljsd.setLastUpdated(wljsd.getDateCreated());
        wljsd.setCreateUser(YgService.getCurrentYg());
        wljsd.setCreateOrgan(YgService.getCurrentYg().getZggs());
        wljsd.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        this.wljsdDao.save(wljsd);
        if (wljsd.getWldList() != null) {
            for (Glfy t : wljsd.getWldList()) {
                Glfy g = this.glfyDao.get(t.getId());
                if (t.getHdsl() != null) {
                    g.setHdsl(Double.valueOf(df.format(t.getHdsl())));
                }
                if (t.getHddj() != null) {
                    g.setHddj(Double.valueOf(df.format(t.getHddj())));
                }
                if (t.getHdje() != null) {
                    g.setHdje(Double.valueOf(df.format(t.getHdje())));
                }
                if (t.getKdse() != null) {
                    g.setKdse(Double.valueOf(df.format(t.getKdse())));
                }
                if (t.getMe() != null) {
                    g.setMe(Double.valueOf(df.format(t.getMe())));
                }
                if (t.getFylb() != null && t.getFylb().getId() != null) {
                    g.setFylb(t.getFylb());
                    g.setSe(Double.valueOf(df2.format((g.getKdse() == null ? 0 : g.getKdse()) * (this.cyfyysDao.get(t.getFylb().getId())).getSl() / 100)));
                }
                g.setCe(Double.valueOf(df2.format((g.getHdje() == null ? 0 : g.getHdje()) - (g.getJe() == null ? 0 : g.getJe()))));
                g.setJsje(Double.valueOf(df2.format((g.getMe() == null ? 0 : g.getMe()) - (g.getSe() == null ? 0 : g.getSe()))));
                g.setZt(1); //0-未结算 1-已结算
                g.setWljsd(wljsd);
                g.setLastUpdated(new Date());
                g.setCreateUser(YgService.getCurrentYg());
                g.setCreateOrgan(YgService.getCurrentYg().getZggs());
                g.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                this.glfyDao.save(g);

                if (g.getGlfymxList() != null && g.getCgd() != null) {
                    List<Cgdmx> cgdmx = g.getCgd().getCgdmxList();
                    List<Glfymx> glfymxList = g.getGlfymxList();
                    Double sumSl = sumSl(cgdmx);
                    Double sumJsjtfy = 0d;
                    for (int i = 0; i < glfymxList.size(); i++) {
                        if (i < glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) / sumSl * glfymxList.get(i).getCgdmx().getRksl())));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                            sumJsjtfy += Double.valueOf(df2.format(glfymx.getJsjtfy()));
                        } else if (i == glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) - sumJsjtfy)));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setLastUpdated(new Date());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                        }
                    }
                } else if (g.getGlfymxList() != null && g.getCsh() != null && g.getCsh() == 1) {
                    List<Glfymx> glfymxList = g.getGlfymxList();
                    Double sumRksl = sumRksl(glfymxList);
                    Double sumJsjtfy = 0d;
                    for (int i = 0; i < glfymxList.size(); i++) {
                        if (i < glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) / sumRksl * glfymx.getRksl())));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                            sumJsjtfy += Double.valueOf(df2.format(glfymx.getJsjtfy()));
                        } else if (i == glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) - sumJsjtfy)));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                        }
                    }
                }
            }
        }
        if (wljsd.getZjbcdList() != null) {
            for (Zjbcd zjbcdTemp : wljsd.getZjbcdList()) {
                Zjbcd zjbcd = this.zjbcdDao.get(zjbcdTemp.getId());
                zjbcd.setZt("ybc");
                zjbcd.setDateCreated(new Date());
                zjbcd.setLastUpdated(zjbcd.getDateCreated());
                zjbcd.setCreateUser(YgService.getCurrentYg());
                zjbcd.setCreateOrgan(YgService.getCurrentYg().getZggs());
                zjbcd.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                zjbcdDao.save(zjbcd);
            }
        }
    }

    /**
     * 修改物流单结算
     *
     * @param wljsd
     */
    @Transactional
    public void updateWljsd(Jsd wljsd) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0000");
        java.text.DecimalFormat df2 = new java.text.DecimalFormat("#0.00");
        Jsd clone = this.findWljsdDetail(wljsd.getId());
        initWljsd(wljsd);
        clone = (Jsd) ReflectionUtils.getInstance(clone, wljsd);
        clone.setZt(0);
        clone.setShzt(0);
        clone.setSqrq(new Date());
        clone.setType(2);
        clone.setLastUpdated(new Date());
        this.wljsdDao.save(clone);
        if (clone.getWldList() != null) {
            for (Glfy t : clone.getWldList()) {
                Glfy g = glfyDao.get(t.getId());
                g.setWljsd(t.getWljsd());
                if (t.getHdsl() != null) {
                    g.setHdsl(Double.valueOf(df.format(t.getHdsl())));
                }
                if (t.getHddj() != null) {
                    g.setHddj(Double.valueOf(df.format(t.getHddj())));
                }
                if (t.getHdje() != null) {
                    g.setHdje(Double.valueOf(df.format(t.getHdje())));
                }
                if (t.getKdse() != null) {
                    g.setKdse(Double.valueOf(df.format(t.getKdse())));
                }
                if (t.getMe() != null) {
                    g.setMe(Double.valueOf(df.format(t.getMe())));
                }
                if (t.getFylb() != null && t.getFylb().getId() != null) {
                    g.setFylb(t.getFylb());
                    g.setSe(Double.valueOf(df2.format((g.getKdse() == null ? 0 : g.getKdse()) * (this.cyfyysDao.get(t.getFylb().getId())).getSl() / 100)));
                }
                g.setCe(Double.valueOf(df2.format((g.getHdje() == null ? 0 : g.getHdje()) - (g.getJe() == null ? 0 : g.getJe()))));
                g.setJsje(Double.valueOf(df2.format((g.getMe() == null ? 0 : g.getMe()) - (g.getSe() == null ? 0 : g.getSe()))));
                g.setWljsd(wljsd);
                //g.setZt(1); //0-未结算 1-已结算
                g.setLastUpdated(new Date());
                this.glfyDao.save(g);

                if (g.getGlfymxList() != null && g.getCgd() != null) {
                    List<Cgdmx> cgdmx = g.getCgd().getCgdmxList();
                    List<Glfymx> glfymxList = g.getGlfymxList();
                    Double sumSl = sumSl(cgdmx);
                    Double sumJsjtfy = 0d;
                    for (int i = 0; i < glfymxList.size(); i++) {
                        if (i < glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) / sumSl * glfymxList.get(i).getCgdmx().getRksl())));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                            sumJsjtfy += Double.valueOf(df2.format(glfymx.getJsjtfy()));
                        } else if (i == glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) - sumJsjtfy)));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                        }
                    }
                } else if (g.getGlfymxList() != null && g.getCsh() != null && g.getCsh() == 1) {
                    List<Glfymx> glfymxList = g.getGlfymxList();
                    Double sumRksl = sumRksl(glfymxList);
                    Double sumJsjtfy = 0d;
                    for (int i = 0; i < glfymxList.size(); i++) {
                        if (i < glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) / sumRksl * glfymx.getRksl())));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                            sumJsjtfy += Double.valueOf(df2.format(glfymx.getJsjtfy()));
                        } else if (i == glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) - sumJsjtfy)));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                        }
                    }
                }
            }
        }

        if (clone.getZjbcdList() != null) {
            for (Zjbcd zjbcdTemp : clone.getZjbcdList()) {
                if (zjbcdTemp.getId() != null) {
                    Zjbcd zjbcd = this.zjbcdDao.get(zjbcdTemp.getId());
                    zjbcd.setLastUpdated(new Date());
                    zjbcd.setDeleted(1);
                } else {
                    zjbcdTemp.setDateCreated(new Date());
                    zjbcdTemp.setLastUpdated(zjbcdTemp.getDateCreated());
                    zjbcdTemp.setCreateUser(YgService.getCurrentYg());
                    zjbcdTemp.setCreateOrgan(YgService.getCurrentYg().getZggs());
                    zjbcdTemp.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                }
            }
        }
    }

    /**
     * 均摊物流单结算
     *
     * @param wljsd
     */
    @Transactional
    public void jtWljsd(Jsd wljsd) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0000");
        java.text.DecimalFormat df2 = new java.text.DecimalFormat("#0.00");
        initWljsd(wljsd);
        if (wljsd.getWldList() != null) {
            for (Glfy t : wljsd.getWldList()) {
                Glfy g = glfyDao.get(t.getId());
                g.setWljsd(t.getWljsd());
                if (t.getHdsl() != null) {
                    g.setHdsl(Double.valueOf(df.format(t.getHdsl())));
                }
                if (t.getHddj() != null) {
                    g.setHddj(Double.valueOf(df.format(t.getHddj())));
                }
                if (t.getHdje() != null) {
                    g.setHdje(Double.valueOf(df.format(t.getHdje())));
                }
                if (t.getKdse() != null) {
                    g.setKdse(Double.valueOf(df.format(t.getKdse())));
                }
                if (t.getMe() != null) {
                    g.setMe(Double.valueOf(df.format(t.getMe())));
                }
                if (t.getFylb() != null && t.getFylb().getId() != null) {
                    g.setFylb(t.getFylb());
                    g.setSe(Double.valueOf(df2.format((g.getSe() == null ? 0 : g.getSe()))));
                }
                g.setCe(Double.valueOf(df2.format((g.getHdje() == null ? 0 : g.getHdje()) - (g.getJe() == null ? 0 : g.getJe()))));
                g.setJsje(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()))));
                g.setWljsd(wljsd);
                //g.setZt(1); //0-未结算 1-已结算
                g.setLastUpdated(new Date());
                this.glfyDao.save(g);

                if (g.getGlfymxList() != null && g.getCgd() != null) {
                    List<Cgdmx> cgdmx = g.getCgd().getCgdmxList();
                    List<Glfymx> glfymxList = g.getGlfymxList();
                    Double sumSl = sumSl(cgdmx);
                    Double sumJsjtfy = 0d;
                    for (int i = 0; i < glfymxList.size(); i++) {
                        if (i < glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) / sumSl * glfymxList.get(i).getCgdmx().getRksl())));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                            sumJsjtfy += Double.valueOf(df2.format(glfymx.getJsjtfy()));
                        } else if (i == glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) - sumJsjtfy)));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                        }
                    }
                } else if (g.getGlfymxList() != null && g.getCsh() != null && g.getCsh() == 1) {
                    List<Glfymx> glfymxList = g.getGlfymxList();
                    Double sumRksl = sumRksl(glfymxList);
                    Double sumJsjtfy = 0d;
                    for (int i = 0; i < glfymxList.size(); i++) {
                        if (i < glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) / sumRksl * glfymx.getRksl())));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                            sumJsjtfy += Double.valueOf(df2.format(glfymx.getJsjtfy()));
                        } else if (i == glfymxList.size() - 1) {
                            Glfymx glfymx = this.glfymxDao.get(glfymxList.get(i).getId());
                            glfymx.setJsjtfy(Double.valueOf(df2.format((g.getJsje() == null ? 0 : g.getJsje()) - sumJsjtfy)));//结算均摊费用
                            glfymx.setType(2); //1-估收均摊 2-结算均摊
                            glfymx.setLastUpdated(new Date());
                            glfymx.setCreateUser(YgService.getCurrentYg());
                            glfymx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                            glfymx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                            glfymx.setDeleted(0);
                            this.glfymxDao.save(glfymx);
                        }
                    }
                }
            }
        }
    }


    private Double sumSl(List<Cgdmx> cgdmxList) {
        Double sumSl = 0d;
        for (Cgdmx cgdmx : cgdmxList) {
            sumSl += cgdmx.getRksl() == null ? 0 : cgdmx.getRksl();
        }
        return sumSl;
    }

    private Double sumRksl(List<Glfymx> glfymxList) {
        Double sumRksl = 0d;
        for (Glfymx glfymx : glfymxList) {
            sumRksl += glfymx.getRksl() == null ? 0 : glfymx.getRksl();
        }
        return sumRksl;
    }

    /**
     * 删除物流单结算
     *
     * @param id
     */
    @Transactional
    public void deleteWljsd(Long id) {
        Jsd wljsd = this.findWljsdDetail(id);
        wljsd.setLastUpdated(new Date());
        wljsd.setDeleted(1);
        this.wljsdDao.save(wljsd);
        if (wljsd.getWldList() != null) {
            for (Glfy t : wljsd.getWldList()) {
                Glfy g = glfyDao.findUniqueByProperty("id", t.getId());
//                for (Glfymx gl : g.getGlfymxList()) {
//                    Glfymx glfymx = this.glfymxDao.findUniqueByProperty("id", gl.getId());
//                    glfymx.setDeleted(1);
//                    this.glfymxDao.save(glfymx);
//                }
                g.setWljsd(null);
                g.setHddj(null);
                g.setHdsl(null);
                g.setHdje(null);
                g.setMe(null);
                g.setKdse(null);
                g.setSe(null);
                g.setJsje(null);
                g.setCe(null);
                g.setZt(0);
                glfyDao.save(g);
            }
            for (Zjbcd z : wljsd.getZjbcdList()) {
                Zjbcd zjbcd = zjbcdDao.findUniqueByProperty("id", z.getId());
                zjbcd.setJsd(null);
                zjbcdDao.save(zjbcd);
            }
        }
    }

    /**
     * 查询物流结算单明细
     *
     * @param id
     * @return
     */
    @Transactional
    public Jsd findWljsdDetail(Long id) {
        return this.wljsdDao.get(id);
    }

    @Transactional
    public String findwldDetail(Long wljsdId) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String result = "";
        Double jfsl = 0d;
        Double je = 0d;
        Double hdsl = 0d;
        Double hdje = 0d;
        Double kdse = 0d;
        Double se = 0d;
        Double me = 0d;
        Double jsje = 0d;
        Double ce = 0d;
        List<Map> wljsdList = new ArrayList<Map>();
        Jsd jsd = this.wljsdDao.get(wljsdId);
        if (jsd.getShzt() == 1) {
            wljsdList = this.wljsdmxDao.findWljsdDetail(wljsdId);
        } else
            wljsdList = this.wljsdDao.findWljsdDetail(wljsdId);
        if (wljsdList.size() > 0) {
            for (Map map : wljsdList) {
                jfsl += map.get("jfsl") == null ? 0d : Double.parseDouble(map.get("jfsl").toString());
                je += map.get("je") == null ? 0d : Double.parseDouble(map.get("je").toString());
                hdsl += map.get("hdsl") == null ? 0d : Double.parseDouble(map.get("hdsl").toString());
                hdje += map.get("hdje") == null ? 0d : Double.parseDouble(map.get("hdje").toString());
                kdse += map.get("kdse") == null ? 0d : Double.parseDouble(map.get("kdse").toString());
                se += map.get("se") == null ? 0d : Double.parseDouble(map.get("se").toString());
                me += map.get("me") == null ? 0d : Double.parseDouble(map.get("me").toString());
                jsje += map.get("jsje") == null ? 0d : Double.parseDouble(map.get("jsje").toString());
                ce += map.get("ce") == null ? 0d : Double.parseDouble(map.get("ce").toString());
            }
            result = "{\"rows\":" + gson.toJson(wljsdList) + ",\"footer\":[{\"sydw.qc\":\"合计:\",\"jfsl\":" + jfsl
                    + ", \"je\":" + je + ",\"se\":" + se + ",\"hdsl\":" + hdsl + ",\"hdje\":" + hdje + ",\"kdse\":" + kdse + ",\"me\":" + me + ",\"jsje\":" + jsje + ", \"ce\":" + ce + "}]}";
        } else {
            result = "{\"rows\":" + gson.toJson(wljsdList) + "}";
        }
        return result;
    }


    /**
     * 审核状态
     *
     * @param id
     * @param status
     */
    @Transactional
    public void changeStatus(Long id, Integer status) {
        Jsd wljsd = this.findWljsdDetail(id);
        wljsd.setZt(status);
        wljsd.setShzt(0);
        this.wljsdDao.save(wljsd);
    }

    /**
     * 物流结算单查询
     */
    @Transactional
    public String findWldSearch(Long sydwGsId, Long wlsId, String wlsqc, String gldh, Long wljsdId, Integer wljsdZt) {
        List<Glfy> glfyList = this.glfyDao.findGlfyList(sydwGsId, wlsId, wlsqc, gldh, wljsdId, wljsdZt);
        if (glfyList.size() > 0) {
            glfyList = getCzztByGlfy(glfyList);
            glfyList = getCgXsDhByGlfy(glfyList);
        }
        String wldJson = BaseUtils.fetchCollectionToJson(".", false, glfyList, "id", "cgd.id", "czzt", "cg_xs_dh", "sydw.id", "sydw.qc", "fylb.id", "fylb.fykmjc", "fylb.sl", "jfsl", "dj", "je", "hdsl", "hddj", "hdje", "kdse", "se", "me", "ce", "jsje", "bz", "zt", "createOrgan.id", "createUser.id", "createUser.xm");
        return "{\"rows\":[" + wldJson + "]}";
    }

    public List<Glfy> getCzztByGlfy(List<Glfy> glfyList) {
        for (Glfy g : glfyList) {
            String czzt = "";
            if ((g.getCsh() != null && g.getCsh() == 1) || (g.getCgd() != null && g.getCgd().getYrk() == true) || (g.getXsd() != null && g.getXsd().getYck() == true)) {
                czzt = "kjs";
            } else {
                czzt = "null";
            }
            g.setCzzt(czzt);
        }
        return glfyList;
    }

    public List<Glfy> getCgXsDhByGlfy(List<Glfy> glfyList) {
        for (Glfy g : glfyList) {
            String cg_xs_dh = "";
            if (g.getCgd() != null && g.getCgd().getCgdh() != null) {
                cg_xs_dh = "" + g.getCgd().getCgdh() + "";
            } else if (g.getXsd() != null && g.getXsd().getXsdh() != null) {
                cg_xs_dh = "" + g.getXsd().getXsdh() + "";
            } else if (g.getGldh() != null) {
                cg_xs_dh = "" + g.getGldh() + "";
            } else {
                cg_xs_dh = "";
            }
            g.setCg_xs_dh(cg_xs_dh);
        }
        return glfyList;
    }

    /**
     * 采购结算单冲销
     *
     * @param ywljsd
     */
    @Transactional
    public void wljsdtz(Jsd ywljsd) {
        Jsd hcwljsd = new Jsd();
        ReflectionUtils.getInstance(hcwljsd, ywljsd);
        initWljsd(hcwljsd);
        hcwljsd.setId(null);
        hcwljsd.setJsdh(this.bmService.getJsdbm("wljs"));
        if (hcwljsd.getDdfy() != null)
            hcwljsd.setDdfy(0 - hcwljsd.getDdfy());
        hcwljsd.setMe(0 - hcwljsd.getMe());
        hcwljsd.setJe(0 - hcwljsd.getJe());
        hcwljsd.setSe(0 - hcwljsd.getSe());
        hcwljsd.setCe(0 - hcwljsd.getCe());
        hcwljsd.setFpze(0 - hcwljsd.getFpze());

        List<Wljsdmx> wljsdmxList = hcwljsd.getWljsdmxList();
        List<Wljsdmx> hcwljsdmxList = new ArrayList<Wljsdmx>();
        for (Wljsdmx wljsdmx : wljsdmxList) {
            Wljsdmx hcwljsdmx = new Wljsdmx();
            ReflectionUtils.getInstance(hcwljsdmx, wljsdmx);
            hcwljsdmx.setId(null);
            hcwljsdmx.setJe(0 - wljsdmx.getJe());
            hcwljsdmx.setSe(0 - wljsdmx.getSe());
            hcwljsdmx.setCe(0 - wljsdmx.getCe());
            hcwljsdmx.setMe(0 - wljsdmx.getMe());
            hcwljsdmx.setKdse(0 - wljsdmx.getKdse());
            hcwljsdmx.setJfsl(0 - wljsdmx.getJfsl());
            hcwljsdmx.setHdje(0 - wljsdmx.getHdje());
            hcwljsdmx.setHdsl(0 - wljsdmx.getHdsl());
            hcwljsdmx.setJsje(0 - wljsdmx.getJsje());
            hcwljsdmx.setJsd(hcwljsd);
            hcwljsdmx.setCreateUser(wljsdmx.getCreateUser());
            hcwljsdmx.setCreateOrgan(wljsdmx.getCreateOrgan());
            hcwljsdmx.setCreateDepartment(wljsdmx.getCreateDepartment());
            hcwljsdmx.setDateCreated(new Date());
            hcwljsdmx.setLastUpdated(hcwljsdmx.getDateCreated());

            List<Jsdfymx> hcjsdfymxList = new ArrayList<Jsdfymx>();
            for (Jsdfymx jsdfymx : wljsdmx.getJsdfymxList()) {
                Jsdfymx hcjsdfymx = new Jsdfymx();
                ReflectionUtils.getInstance(hcjsdfymx, jsdfymx);
                hcjsdfymx.setId(null);
                if (jsdfymx.getRksl() != null)
                    hcjsdfymx.setRksl(0 - jsdfymx.getRksl());
                hcjsdfymx.setJtfy(0 - jsdfymx.getJtfy());
                if (jsdfymx.getJsjtfy() != null)
                    hcjsdfymx.setJsjtfy(0 - jsdfymx.getJsjtfy());
                hcjsdfymx.setWljsdmx(hcwljsdmx);
                this.jsdfymxDao.save(hcjsdfymx);
                hcjsdfymxList.add(hcjsdfymx);
            }
            hcwljsdmx.setJsdfymxList(hcjsdfymxList);
            this.wljsdmxDao.save(hcwljsdmx);
            hcwljsdmxList.add(hcwljsdmx);
        }
        hcwljsd.setWljsdmxList(hcwljsdmxList);


        List<Glfy> glfyList = ywljsd.getWldList();
        for (int i = 0; i < glfyList.size(); i++) {
            Glfy glfy = glfyList.get(i);
            glfy.setZt(0);
            glfy.setWljsd(null);
            this.glfyDao.save(glfy);
        }

        hcwljsd.setSftzd(true);
        hcwljsd.setYjsd(ywljsd);
        hcwljsd.setCgdList(null);
        hcwljsd.setCgdmxList(null);
        hcwljsd.setXsdList(null);
        hcwljsd.setXsdmxList(null);
        hcwljsd.setWldList(null);
        hcwljsd.setCgjsdmxList(null);
        hcwljsd.setXsjsdmxList(null);
        hcwljsd.setZjbcdList(null);
        hcwljsd.setAcctyear(null);
        hcwljsd.setUuid(null);
        hcwljsd.setVoucherId(null);
        hcwljsd.setVoucherNum(null);
        hcwljsd.setInsertFlag(1);
        hcwljsd.setDeleteFlag(1);
        hcwljsd.setCreateUser(ywljsd.getCreateUser());
        hcwljsd.setCreateOrgan(ywljsd.getCreateOrgan());
        hcwljsd.setCreateDepartment(ywljsd.getCreateDepartment());
        hcwljsd.setDateCreated(new Date());
        hcwljsd.setLastUpdated(hcwljsd.getDateCreated());
        hcwljsd.setSqrq(hcwljsd.getDateCreated());
        hcwljsd.setKprq(hcwljsd.getDateCreated());
        this.wljsdDao.save(hcwljsd);

        ywljsd.setCgdList(null);
        ywljsd.setCgdmxList(null);
        ywljsd.setXsdList(null);
        ywljsd.setXsdmxList(null);
        ywljsd.setWldList(null);
        ywljsd.setCgjsdmxList(null);
        ywljsd.setXsjsdmxList(null);
        ywljsd.setZjbcdList(null);
        ywljsd.setSftzd(true);
        ywljsd.setLastUpdated(new Date());
        this.wljsdDao.save(ywljsd);
    }

    /**
     * 关联判断
     *
     * @param wljsd
     */

    private void initWljsd(Jsd wljsd) {
        if (wljsd != null && wljsd.getGlqy() != null && wljsd.getGlqy().getQyfr() != null && wljsd.getGlqy().getQyfr().getId() == null)
            wljsd.getGlqy().setQyfr(null);
        if (wljsd != null && wljsd.getSqr() != null && wljsd.getSqr().getId() == null)
            wljsd.setSqr(null);
        if (wljsd != null && wljsd.getGlqy() != null && wljsd.getGlqy().getId() == null)
            wljsd.setGlqy(null);
        if (wljsd != null && wljsd.getGs() != null && wljsd.getGs().getId() == null)
            wljsd.setGs(null);
        if (wljsd != null && wljsd.getMe() == null)
            wljsd.setMe(null);
        if (wljsd != null && wljsd.getJe() == null)
            wljsd.setJe(null);
        if (wljsd != null && wljsd.getSe() == null)
            wljsd.setSe(null);
        if (wljsd != null && wljsd.getCe() == null)
            wljsd.setCe(null);
    }

    @Autowired
    public void setGlfyDao(GlfyDao glfyDao) {
        this.glfyDao = glfyDao;
    }

    @Autowired
    public void setWljsdDao(WljsdDao wljsdDao) {
        this.wljsdDao = wljsdDao;
    }

    @Autowired
    public void setWljsdmxDao(WljsdmxDao wljsdmxDao) {
        this.wljsdmxDao = wljsdmxDao;
    }

    @Autowired
    public void setJsdfymxDao(JsdfymxDao jsdfymxDao) {
        this.jsdfymxDao = jsdfymxDao;
    }

    @Autowired
    public void setCyfyysDao(CyfyysDao cyfyysDao) {
        this.cyfyysDao = cyfyysDao;
    }

    @Autowired
    public void setGlfymxDao(GlfymxDao glfymxDao) {
        this.glfymxDao = glfymxDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setZjbcdDao(ZjbcdDao zjbcdDao) {
        this.zjbcdDao = zjbcdDao;
    }
}
